package com.goparry.taskman.search;

import java.util.List;

import android.app.SearchManager;
import android.content.Intent;

import com.goparry.taskman.browse.*;
import com.goparry.taskman.repository.RObject;
import com.goparry.taskman.repository.RObject.SortBy;
import com.goparry.taskman.restclient.RESTUtilities;

public class SearchActivity extends BrowseActivity {
	String mSearchCriteria="";

	@Override
	public List<RObject> onInitialSelection() {
		// This gets the initial list of objects, in this case the vaults.
		String uri="";
		if (getIntent().getAction().equals(Intent.ACTION_SEARCH)) {
			mSearchCriteria = getIntent().getStringExtra(SearchManager.USER_QUERY);
			uri = RESTUtilities.getDocumentsByNameURI(mSearchCriteria);
		} else if (getIntent().getAction().equals("BARCODE_SEARCH")) {
			mSearchCriteria = getIntent().getStringExtra("BARCODE");
			// The returned searchTerm is of the form <code>:<ID>, i.e. D:E-002341 where
			// code is :	D - for document
			//			A - for asset code
			//			T - for Tag id
			String items[] = mSearchCriteria.split(":");
			String code = items[0];
			mSearchCriteria = items[1];
			if (code.equalsIgnoreCase("T")) {
				uri = RESTUtilities.getDocumentsByTagIdURI(mSearchCriteria);
				mSearchCriteria = "Tag ID=" + mSearchCriteria;
			} else if (code.equalsIgnoreCase("A")) {
				uri = RESTUtilities.getDocumentsByAssetCodeURI(mSearchCriteria);
				mSearchCriteria = "Asset Code=" + mSearchCriteria;
			} else {
				uri = RESTUtilities.getDocumentsByNameURI(mSearchCriteria);				
				mSearchCriteria = "Name=" + mSearchCriteria;
			}
		} else if (getIntent().getAction().equals("GEO_SEARCH")) {
			mSearchCriteria = getIntent().getStringExtra("GEO_LOCATION");
			uri = RESTUtilities.getDocumentsByGeoURI(mSearchCriteria);
			mSearchCriteria = "GEO=" + mSearchCriteria;
		}
		setExtraString(mSearchCriteria);
		List<RObject> rObjects = null;
    	rObjects = getRObjects(uri);
        RObject.sortBy = SortBy.NAME;
		return rObjects;
	}

}
